package flc.ast.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyDayBean;
import stark.common.basic.view.container.StkLinearLayout;

public class CountdownAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MyDayBean> mDatas = new ArrayList<>();//数据
    private final Context mContext;

    public CountdownAdapter(List<MyDayBean> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public CountdownAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmDatas(List<MyDayBean> mDatas) {
        this.mDatas = mDatas;
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(MyDayBean bean, int position);

        void onClickDelete(MyDayBean bean, int position);
    }

    private OnItemClickListener mListener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_countdown, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).tvName.setText(mDatas.get(position).getTitle());

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            long timeSpan = TimeUtils.getTimeSpan(mDatas.get(position).getDate(), TimeUtils.getNowString(format), format, TimeConstants.DAY);
            if (timeSpan < 0) {//小于0表示今年这天已经过了
                timeSpan = Math.abs(timeSpan);
                ((ItemHolder) holder).tvTip.setText(R.string.past_text);
            } else {
                ((ItemHolder) holder).tvTip.setText(R.string.remain_text);
            }
            ((ItemHolder) holder).tvInterval.setText(timeSpan + "");
            ((ItemHolder) holder).tvDate.setText(mDatas.get(position).getDate());
            ((ItemHolder) holder).ivImg.setImageResource(mDatas.get(position).getIcon());

            ((ItemHolder) holder).llDelete.setOnClickListener(v ->
                    mListener.onClickDelete(mDatas.get(position), position));
            holder.itemView.setOnClickListener(v ->
                    mListener.onClick(mDatas.get(position), position));
        } else {

        }
    }

    //多出尾部刷新的item
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //item的holder
    static class ItemHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tvTip, tvInterval, tvDate;
        private ImageView ivImg;
        private StkLinearLayout llDelete;

        public ItemHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvCountdownItemName);
            tvTip = itemView.findViewById(R.id.tvCountdownItemTip);
            tvInterval = itemView.findViewById(R.id.tvCountdownItemInterval);
            tvDate = itemView.findViewById(R.id.tvCountdownItemDate);

            ivImg = itemView.findViewById(R.id.ivCountdownItemImg);

            llDelete = itemView.findViewById(R.id.llCountdownItemDelete);
        }
    }

}

